package com.cxh.rmsq.ui.fragment.self

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.db.model.CommunityContent
import com.cxh.rmsq.db.model.JoinRecord
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.net.newNet.ProjectViewModel
import com.cxh.rmsq.net.newNet.TreasureViewModel
import com.cxh.rmsq.ui.adapter.self.CircleListAdapter
import com.cxh.rmsq.ui.adapter.self.OrderListAdapter
import com.cxh.rmsq.ui.fragment.BaseFragment
import com.luck.picture.lib.entity.LocalMedia
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

private const val ARG_PARAM2 = "param2"


class OrderListFragment : BaseFragment(), OrderListAdapter.OnCustomClickListener {
    private var param2: String? = null
    private lateinit var refreshList: SmartRefreshLayout
    private lateinit var recyclerView: RecyclerView
    private var listAdapter: OrderListAdapter? = null
    private var page = 1

    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(TreasureViewModel::class.java)
    }

    companion object {
        fun newInstance(param1: String): OrderListFragment {
            val fragment = OrderListFragment()
            val args = Bundle()
            args.putString(ARG_PARAM2, param1)
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.fragment_order_list

    }

    override fun onInitView(savedInstanceState: Bundle?, intent: Intent?) {
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        param2 = requireArguments().getString(ARG_PARAM2)

        refreshList = view.findViewById(R.id.refresh_list) as SmartRefreshLayout
        recyclerView = view.findViewById(R.id.rv_list) as RecyclerView

        recyclerView.layoutManager = LinearLayoutManager(context)
        listAdapter = OrderListAdapter(1, this) //
        listAdapter!!.setEmptyView(View.inflate(activity, R.layout.empty_data_layout, null))
        recyclerView.adapter = listAdapter
        listAdapter!!.setOnItemClickListener { adapter, view, position ->

        }


        refreshList.resetNoMoreData()
        refreshList.setRefreshHeader(MaterialHeader(activity))
        refreshList.setRefreshFooter(ClassicsFooter(activity))
        refreshList.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                getData()
                refreshLayout.finishLoadMore()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
//                dataList.clear()
                listAdapter!!.setNewInstance(null)
                getData()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        page = 1
        listAdapter!!.setNewInstance(null)
        getData()

    }

    private fun getData() {
        viewModel.getOrderList(param2.toString(), page, 10) {
            Log.e("total", it.data.total.toString() + "")
            if (refreshList.isRefreshing) {
                refreshList.finishRefresh()
            }
            if (it.data != null) {
                if (page == 1) {
                    listAdapter!!.setNewInstance(it.data.list)
                    if (it.data.list!!.size < 10) {
                        refreshList.setEnableLoadMore(false)
                    } else {
                        refreshList.setEnableLoadMore(true)
                    }
                } else {
                    if (it.data.list!!.size > 0) {
                        listAdapter!!.addData(it.data.list!!)
                        refreshList.setEnableLoadMore(true)
                    } else {
                        refreshList.setEnableLoadMore(false)
                        refreshList.finishLoadMoreWithNoMoreData()
                    }
                }

                listAdapter!!.notifyDataSetChanged()
            }
        }
    }

    override fun onItemClick(position: Int, data: JoinRecord?) {

    }

    override fun onItemLongClick(position: Int, data: JoinRecord?) {

    }

}